﻿using System;
using System.Collections.Generic;
using System.Linq;
using Lu.Blog.Domain.Model.Archive;
using Lu.Blog.Domain.Model.DocumentType;
using umbraco;
using umbraco.interfaces;
using umbraco.MacroEngines;
using Vega.USiteBuilder;

namespace Lu.Blog.Domain.Utils
{
    public class BlogContentHelper
    {
        public static IEnumerable<Category> GetAllCategories()
        {
            return
                GetRoot().GetDescendantNodes().Where(i => i.NodeTypeAlias == "Category")
                    .Select(i => ContentHelper.GetByNodeId<Category>(i.Id));
        }

        public static IEnumerable<Category> GetAllNonEmptyCategories()
        {
            return
                GetRoot().GetDescendantNodes().Where(i => i.NodeTypeAlias == "Category" && i.ChildrenAsList.Any())
                    .Select(i => ContentHelper.GetByNodeId<Category>(i.Id));
        }

        public static IEnumerable<INode> GetAllPost()
        {
            return GetAllPost(GetRoot());
        }

        public static IEnumerable<INode> GetAllPost(INode d)
        {
            var ret = new List<INode>();
            foreach (INode node in d.ChildrenAsList)
            {
                ret.Add(node);
                if (node.ChildrenAsList.Count() != 0)
                {
                    ret.AddRange(GetAllPost(node));
                }
            }
            return ret;
        }

        public static INode GetRoot()
        {
            return new DynamicNode(-1);
        }

        public static IEnumerable<ArchiveMonth> GetArchives(INode categoryNode)
        {
            var retDico = new Dictionary<string, ArchiveMonth>();
            IOrderedEnumerable<INode> categoryPosts = GetAllPost(categoryNode).OrderBy(c => c.CreateDate);

            foreach (INode post in categoryPosts)
            {
                ArchiveMonth archive = null;

                var postContentPage = ContentHelper.GetByNodeId<ContentPage>(post.Id);
                DateTime date;
                if (postContentPage.PostDate != DateTime.MinValue)
                {
                    date = new DateTime(postContentPage.PostDate.Year, postContentPage.PostDate.Month, 1, 0, 0, 0);
                }
                else
                {
                    date = new DateTime(postContentPage.CreateDate.Year, postContentPage.CreateDate.Month, 1, 0, 0, 0);
                }

                string key = date.ToString("yyyy-MM");
                if (retDico.ContainsKey(key))
                {
                    retDico.TryGetValue(key, out archive);
                }
                if (archive == null)
                {
                    archive = new ArchiveMonth
                    {
                        Date = date
                    };
                }

                archive.Posts.Add(post);

                if (!retDico.ContainsKey(key))
                {
                    retDico.Add(key, archive);
                }
            }

            return retDico.Values.OrderByDescending(am => am.Date);
        }
    }
}